﻿using System;
using System.Data;
using System.Globalization;
using System.Threading;
using System.Web.UI;
using sdamarketing.App_Code;

namespace sdamarketing
{
    public partial class Default : BasePageNMP
    {


        protected void Page_Load(object sender, EventArgs e)
        {
           // Page.Master.FindControl("AdministrationPanel").Visible = false;
        }

        protected void LogInButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (languagesDD.SelectedValue!="-1")
                {
                    
                DataSet dataSet = Users.authenticateUser(username.Text, password.Text);
                // proveruvam dali e blokiran ili izbrisan userot (ima edno flagce vo baza isBlock 
                if (dataSet.Tables["User"].Rows[0].ItemArray[1].ToString() == "False" && dataSet.Tables["User"].Rows[0].ItemArray[2].ToString() == "False")
                {
                    
                    Session["idUser"] = dataSet.Tables["User"].Rows[0].ItemArray[0].ToString();
                    Session["username"] = username.Text;
                    Session["idRole"] = dataSet.Tables["User"].Rows[0].ItemArray[3].ToString();
                    Response.Redirect("~/Account/Home.aspx");
                }
                }
                else
                {
                    Thread.CurrentThread.CurrentUICulture = new CultureInfo("mk-MK");
                    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("mk-MK");

                    Session["MyCulture"] = "mk-MK";
                    Session["MyUICulture"] = "mk-MK";
                    DataSet dataSet = Users.authenticateUser(username.Text, password.Text);
                    // proveruvam dali e blokiran ili izbrisan userot (ima edno flagce vo baza isBlock 
                    if (dataSet.Tables["User"].Rows[0].ItemArray[1].ToString() == "False" && dataSet.Tables["User"].Rows[0].ItemArray[2].ToString() == "False")
                    {

                        Session["idUser"] = dataSet.Tables["User"].Rows[0].ItemArray[0].ToString();
                        Session["username"] = username.Text;
                        Session["idRole"] = dataSet.Tables["User"].Rows[0].ItemArray[3].ToString();
                        Response.Redirect("~/Account/Home.aspx");
                    }
                }
            }                        
            catch (Exception)
            {
                if (Session["MyCulture"].ToString() == "en-US")
                {
                    lblError.Text = "Username or password invalid";
                }
                else
                {
                    lblError.Text = "Корисничкото име и/или лозинката е/се погрешни";
                }
            }
        }

        protected void languagesDD_SelectedIndexChanged(object sender, EventArgs e)
        {
            string lang = languagesDD.SelectedValue;
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang);

            Session["MyCulture"] = languagesDD.SelectedValue;
            Session["MyUICulture"] = languagesDD.SelectedValue;

            //Response.Redirect(Request.RawUrl);         
        }
    }
}
